{extend name="public:layout" /}
{block name="title"}添加赠品组合{/block}
{block name="head"}{/block}
{block name="body"}
<div class="wrapper wrapper-content">
    <div class="ibox">
        <div class="ibox-title">
            <ol class="breadcrumb">
                <li><i class="fa fa-hand-o-right"></i> <a href="{:url('index')}"> 赠品组合列表</a></li>
                <li class="active">添加赠品组合</li>
            </ol>
        </div>
        <div class="ibox-content">
            <div class="alert alert-warning">
                <strong>说明：</strong>
                <p>1.请不要重复选择同一个商品。</p>
                <p>2.赠品为正常销售中的商品，赠品送出后会记录销量。</p>
                <p>3.如赠品库存不足或已经下架，不会被作为赠品加入到订单流程。</p>
            </div>
            <form method="post" class="form-horizontal" id="form">
                <div class="form-group">
                    <label class="col-sm-2 control-label Validform_label">主卖商品</label>
                    <div class="col-sm-10">
                        <a class="btn btn-info" onclick="showOneGoods(this,setOneGoods);"
                           data-field="sku_id"
                           data-siteid="{$site_id}"
                           data-url="{:url('plus/Goods/selectone')}"
                        >选择主商品</a>
                        <div id="goodsinfo"></div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label Validform_label">赠品选择</label>
                    <div class="col-sm-10">
                        <a class="btn btn-info" onclick="showMoreGoods(this,setMoreGoods);"
                           data-field="sku_id"
                           data-siteid="{$site_id}"
                           data-url="{:url('plus/Goods/selectmore')}"
                        >选择赠品</a>
                        <div id="goodslist"></div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-10 col-sm-offset-2">
                        <button class="btn btn-primary" type="button" id="btnSub">保存内容</button>
                        <a href="{:url('index')}" class="btn btn-white">取消</a>
                        <span id="validError"></span>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script id="set-goodsinfo-tpl" type="text/html">
    <table class="table table-bordered">
        <thead>
        <tr>
            <th width="50">#</th>
            <th width="300">商品名称</th>
            <th width="100">销售价</th>
            <th width="100">库存</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>{{ sku_info.sku_id }}</td>
            <td>
                <img src="{{ sku_info.goods_thumb }}" class="pull-left" style="width:32px;height:32px;margin:4px;" />
                {{ sku_info.fulltitle }}
            </td>
            <td>{{ sku_info.simple_price }}元</td>
            <td>{{ sku_info.goods_stock }}</td>
        </tr>
        </tbody>
    </table>
    <input type="hidden" name="site_id" value="{{ sku_info.site_id }}" />
    <input type="hidden" name="site_name" value="{{ sku_info.site_name }}" />
    <input type="hidden" name="goods_id" value="{{ sku_info.goods_id }}" />
    <input type="hidden" name="sku_id" value="{{ sku_info.sku_id }}" />
    <input type="hidden" name="goods_name" value="{{ sku_info.goods_name }}" />
    <input type="hidden" name="sku_name" value="{{ sku_info.sku_name }}" />
    <input type="hidden" name="goods_thumb" value="{{ sku_info.goods_thumb }}" />
</script>

<script id="set-goodslist-tpl" type="text/html">
    <table class="table table-bordered">
        <thead>
        <tr>
            <th width="50">#</th>
            <th width="300">商品名称</th>
            <th width="100">销售价格</th>
            <th width="100">库存</th>
            <th width="100">赠送数量</th>
            <th width="40">操作</th>
        </tr>
        </thead>
        <tbody>
        {{ each sku_list }}
        <tr>
            <td>
                <input type="hidden" name="sku_list[{{ $value.sku_id }}][gift_goods_id]" value="{{ $value.goods_id }}" />
                <input type="hidden" name="sku_list[{{ $value.sku_id }}][gift_sku_id]" value="{{ $value.sku_id }}" />
                <input type="hidden" name="sku_list[{{ $value.sku_id }}][gift_goods_name]" value="{{ $value.goods_name }}" />
                <input type="hidden" name="sku_list[{{ $value.sku_id }}][gift_sku_name]" value="{{ $value.sku_name }}" />
                <input type="hidden" name="sku_list[{{ $value.sku_id }}][gift_goods_thumb]" value="{{ $value.goods_thumb }}" />
                {{ $value.sku_id }}
            </td>
            <td>
                <img src="{{ $value.goods_thumb }}" class="pull-left" style="width:32px;height:32px;margin:4px;" />
                {{ $value.goods_name }}
            </td>
            <td>{{ $value.simple_price }}元</td>
            <td>{{ $value.goods_stock }}</td>
            <td><input type="text" class="form-control" name="sku_list[{{ $value.sku_id }}][gift_num]" value="1" datatype="n" /></td>
            <td><a href="javascript:;" onclick="delGoods(this)">移除</a></td>
        </tr>
        {{ /each }}
        </tbody>
    </table>
</script>
{/block}
{block name="script"}
<script type="text/javascript">
    $(function () {
        In('selectgoods');
        ajaxSubForm('#btnSub');
    })
    function setOneGoods(field,sku){
        if(sku.sku_id>0){
            In('templete',function () {
                var html = template('set-goodsinfo-tpl', sku);
                $('#goodsinfo').html(html);
            })
        }
    }
    //选取赠品后回调
    function setMoreGoods(field,sku) {
        In('templete',function () {
            var html = template('set-goodslist-tpl', sku);
            $('#goodslist').html(html);
        })
    }
    function delGoods(obj) {
        $(obj).closest('tr').remove();
    }
</script>
{/block}